package org.zachary.commonservice.controller;

import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.zachary.commonmodule.entity.entity.Result;
import org.zachary.commonservice.entity.*;
import org.zachary.commonservice.service.CreditContractRecordService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dashboard")
public class CreditContractRecordController {

    private final CreditContractRecordService creditContractRecordService;

    public CreditContractRecordController(CreditContractRecordService creditContractRecordService) {
        this.creditContractRecordService = creditContractRecordService;
    }

    @GetMapping("/amount_count")
    public Result<List<CreditCategoryStatsDTO>> getCreditStats() {
        try {
            List<CreditCategoryStatsDTO> stats = creditContractRecordService.getCategoryStats();
            return Result.success(stats);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("统计失败");
        }
    }
    @GetMapping("/amount_count_by_county")
    public Result<List<CreditCategoryStatsDTO>> getCreditStatsByCounty(@RequestParam(required = false, defaultValue = "全部") String county) {
        try {
            List<CreditCategoryStatsDTO> stats = creditContractRecordService.getCategoryStatsByCounty(county);
            return Result.success(stats);
        } catch (Exception e) {
            log.error("获取信用服务统计失败", e);
            return Result.error("统计失败");
        }
    }

    @GetMapping("/people_count")
    public Result<Integer> countAllUsers() {
        try {
            return Result.success(creditContractRecordService.countAllUsers());
        } catch (Exception e) {
            return Result.error("统计失败");
        }
    }
    @GetMapping("/completion_rate")
    public Result<Double> getCompletionRate() {
        try {
            double rate = creditContractRecordService.getCompletionRate();
            return Result.success(rate);
        } catch (Exception e) {
            log.error("获取守约率失败", e);
            return Result.error("获取守约率失败");
        }
    }
    @GetMapping("/top_users")
    public Result<List<DimensionTopUserVO>> getTopUsersByDimension() {
        List<DimensionTopUserVO> list = creditContractRecordService.getTopUsersByDimension();
        return Result.success(list);
    }


    @GetMapping("/use_count")
    public Result<Integer> countAllUsing(){
        try{
            return Result.success(creditContractRecordService.countAllUsing());
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return Result.error("统计失败");
        }
    }
    @GetMapping("/income_count")
    public Result<List<CountyIncomeDTO>> getIncomeByCounty() {
        return Result.success(creditContractRecordService.getIncomeByCounty());
    }
    @GetMapping("/credit_score")
    public Result<List<DistrictScoreStatsDTO>> getScoreBoxPlotData() {
        return Result.success(creditContractRecordService.getDistrictScoreStats());
    }
    @GetMapping("/latest")
    public Result<List<CreditRecordSimpleDTO>> getLatestRecords() {
        try {
            List<CreditRecordSimpleDTO> records = creditContractRecordService.getLatestRecords();
            return Result.success(records);
        } catch (Exception e) {
            log.error("查询守约记录失败", e);
            return Result.error("查询失败");
        }
    }

}
